package hotel.menu.adapter;

import hotel.menu.activity.WaiMai;
import hotel.menu.adapter.WaiMaiPageTwoAdapter.MyRunnable;
import hotel.menu.domain.Food;
import hotel.menu.domain.OrderMessage;
import hotel.menu.net.NetUtil;

import java.util.List;
import com.example.youknow.R;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WaiMaiPageThreeAdapter extends BaseAdapter {
	WaiMai context;
	LayoutInflater layoutInflater;
	List<OrderMessage> waitorder;
	private final int AGAIN = 0001;
	public static OrderMessage waitOrder;
	private ProgressDialog progressDialog;
	private int index;

	public WaiMaiPageThreeAdapter(WaiMai context, List<OrderMessage> orders) {
		this.context = context;
		this.waitorder = orders;
		layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return waitorder.size();
	}

	@Override
	public Object getItem(int position) {
		return getItem(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.waimai_page3_item,
					null);
			viewHolder = new ViewHolder();
			viewHolder.addtime = (TextView) convertView
					.findViewById(R.id.waimai_page3_item_addtime);
			viewHolder.shopname = (TextView) convertView
					.findViewById(R.id.waimai_page3_item_shopname);
			viewHolder.list = (TextView) convertView
					.findViewById(R.id.waimai_page3_item_list);
			viewHolder.address = (TextView) convertView
					.findViewById(R.id.waimai_page3_item_address);
			viewHolder.allprice = (TextView) convertView
					.findViewById(R.id.waimai_page3_item_allprice);
			viewHolder.btn = (TextView) convertView

			.findViewById(R.id.waimai_page3_item_btn);

			viewHolder.ordersn = (TextView) convertView
					.findViewById(R.id.waimai_page3_item_ordersn);
			viewHolder.remark = (TextView) convertView
					.findViewById(R.id.waimai_page3_item_remark);
			viewHolder.remark_ly = (LinearLayout) convertView
					.findViewById(R.id.waimai_page3_item_remark_ly);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();

		}
		viewHolder.btn.setTag(AGAIN);
		viewHolder.btn.setOnClickListener(new MyListener(position));
		OrderMessage waitOrder = waitorder.get(position);
		viewHolder.addtime.setText("下单时间：" + waitOrder.getAddtime());
		viewHolder.shopname.setText(waitOrder.getShopname());
		viewHolder.ordersn.setText(waitOrder.getOrdersn());
		viewHolder.list.setText(getDate(waitOrder.getOrderdetail()));
		viewHolder.address.setText(waitOrder.getSendaddress());
		String remark = waitOrder.getRemark().trim();
		if (remark.length() <= 0) {
			viewHolder.remark_ly.setVisibility(8);
		} else {
			viewHolder.remark_ly.setVisibility(0);
			viewHolder.remark.setText(remark);
		}
		viewHolder.btn.setOnClickListener(new MyListener(position));
		viewHolder.allprice.setText("¥" + waitOrder.getAllprice());

		return convertView;
	}

	private CharSequence getDate(List<Food> orderdetail) {
		StringBuffer buffer = null;
		if (buffer == null) {
			buffer = new StringBuffer();
		} else {
			buffer.delete(0, buffer.length() - 1);
		}
		for (int i = 0; i < orderdetail.size(); i++) {
			if (i != orderdetail.size() - 1) {
				buffer.append(orderdetail.get(i).getDishname() + " ("
						+ orderdetail.get(i).getCount() + "份)、");
			} else {
				buffer.append(orderdetail.get(i).getDishname() + " ("
						+ orderdetail.get(i).getCount() + "份)");
			}

		}
		return buffer.toString();
	}

	class MyListener implements android.view.View.OnClickListener {
		int position;

		MyListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			index = position;
			waitOrder = waitorder.get(index);
			if ((Integer) v.getTag() == AGAIN) {

				AlertDialog.Builder builder = new Builder(context);
				builder.setMessage("是都再吃一次");
				builder.setTitle("提示");
				builder.setPositiveButton("确定",
						new android.content.DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								progressDialog = ProgressDialog.show(context,
										null, "请等待……", true, false);
								new Thread(new MyRunnable(AGAIN)).start();
							}
						});
				builder.setNegativeButton("取消", null);
				builder.show();

			}
		}
	};

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			boolean suc = (Boolean) msg.obj;
			progressDialog.dismiss();
			if (msg.what == AGAIN) {
				if (suc) {
					Toast.makeText(context, "再吃成功", 1).show();
				} else {
					Toast.makeText(context, "服务器繁忙，请稍后再试", 1).show();
				}

			}
		}
	};

	class MyRunnable implements Runnable {
		int mark;

		MyRunnable(int mark) {
			this.mark = mark;
		}

		@Override
		public void run() {
			if (mark == AGAIN) {
				boolean suc = NetUtil.getSingleNetUtil().SendWaitOrder(
						waitOrder.getId(), "init", "buyer");
				handler.sendMessage(handler.obtainMessage(AGAIN, suc));

			}
		}
	}

	public static class ViewHolder {
		public TextView addtime, shopname, list, address, allprice, btn,
				remark, ordersn;
		public LinearLayout remark_ly;
	}

}
